package com.yuejian_06.yuejian_06.activity;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.yuejian_06.yuejian_06.R;
import com.yuejian_06.yuejian_06.fragment.ActivityFragment;
import com.yuejian_06.yuejian_06.fragment.CoachFragment;
import com.yuejian_06.yuejian_06.fragment.ContestFragment;
import com.yuejian_06.yuejian_06.fragment.OAFragment;
import com.yuejian_06.yuejian_06.fragment.OCFragment;

public class OrderActivity extends BaseActivity implements View.OnClickListener   {
    private LinearLayout TabA;
    private LinearLayout TabC;

    private ImageView ImgA;
    private ImageView ImgC;

    private Fragment fragment_oa;
    private Fragment fragment_oc;

    private ImageButton back;
    @Override
    void initViews() {

    }

    @Override
    void initEvents() {

    }

    @Override
    void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);
        initView();//初始化事件
        initEvent();//监听
        setSelect(0);//选择了一个默认的fragment
    }

    private void initView()
    {
        TabA = (LinearLayout) findViewById(R.id.id_tab_activity);
        TabC = (LinearLayout) findViewById(R.id.id_tab_course);

        ImgA = (ImageView) findViewById(R.id.id_tab_activity_img);
        ImgC = (ImageView) findViewById(R.id.id_tab_course_img);

        back = (ImageButton) findViewById(R.id.back);
    }

    private void initEvent()
    {
        TabA.setOnClickListener(this);
        TabC.setOnClickListener(this);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {finish();
            }
        });
    }

    private void resetImgs()
    {
        ImgA.setImageResource(R.drawable.tab_activity_n);
        ImgC.setImageResource(R.drawable.tab_contest_n);
    }

    private void setSelect(int i)
    {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);
        switch (i)
        {
            case 0:
                if (fragment_oa == null)
                {
                    fragment_oa = new OAFragment();
                    transaction.add(R.id.id_order_content, fragment_oa);
                } else
                {
                    transaction.show(fragment_oa);
                }
                ImgA.setImageResource(R.drawable.tab_activity_p);
                break;

            case 1:
                if (fragment_oc == null)
                {
                    fragment_oc = new OCFragment();
                    transaction.add(R.id.id_order_content, fragment_oc);
                } else
                {
                    transaction.show(fragment_oc);
                }
                ImgC.setImageResource(R.drawable.tab_contest_p);
                break;


            default:
                break;
        }

        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction)
    {
        if (fragment_oa != null)
        {
            transaction.hide(fragment_oa);
        }
        if (fragment_oc != null)
        {
            transaction.hide(fragment_oc);
        }
    }

    @Override
    public void onClick(View v) {
        resetImgs();
        switch (v.getId())
        {
            case R.id.id_tab_activity:
                setSelect(0);
                break;
            case R.id.id_tab_course:
                setSelect(1);
                break;

            default:
                break;
        }
    }
}
